import 'package:flutter/material.dart';
import 'package:flutter_app_05/page/calculator.dart';
import 'package:flutter_app_05/page/calculator_page.dart';
import 'package:flutter_app_05/page/index.dart';
import 'package:flutter_app_05/page/login/login.dart';
import 'package:flutter_app_05/page/mine.dart';
import 'package:flutter_app_05/page/more.dart';
import 'package:flutter_app_05/page/pagecal.dart';
import 'package:flutter_app_05/utils/ScreenAdapter.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main(){
  runApp(BottomNavigationWidget());
}

class BottomNavigationWidget extends StatefulWidget {
  @override
  _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
}

class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  @override
  final _bottomColor = Colors.blue;
  int a=0;
  List<Widget>list = [];
  int _currentIndex = 0;
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      body: list[_currentIndex],
        bottomNavigationBar: new BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
                icon: Image(
                  color: (_currentIndex != 0)?Colors.grey:Colors.black,
                    height: ScreenAdapter.height(50.0),
                    width: ScreenAdapter.width(50.0),
                  image: (_currentIndex != 0)
                      ? AssetImage('images/icon_home_true.png'):AssetImage('images/icon_home_false.png',)),
                label: "首页"
            ),
            BottomNavigationBarItem(
               icon: Image(
    color: (_currentIndex != 1)?Colors.grey:Colors.black,
                   height: ScreenAdapter.height(50.0),
                   width: ScreenAdapter.width(50.0),
    image: (_currentIndex != 1)
    ? AssetImage('images/icon_cal_false.png',):AssetImage('images/icon_cal_true.png')),
                label: "计算器"
            ),
            BottomNavigationBarItem(
                icon: Image(
                  color: (_currentIndex != 2)?Colors.grey:Colors.black,
                  height: ScreenAdapter.height(50.0),
                  width: ScreenAdapter.width(50.0),
                  image: (_currentIndex != 2)
                      ? AssetImage('images/icon_more_false.png',)
                      : AssetImage('images/icon_more_true.png'),),
                label: "更多"
            ),
            BottomNavigationBarItem(
                icon: Image(
                  color: (_currentIndex != 3)?Colors.grey:Colors.black,
                  height: ScreenAdapter.height(50.0),
                  width: ScreenAdapter.width(50.0),
                  image: (_currentIndex != 3)
                      ? AssetImage('images/icon_my_false.png',)
                      : AssetImage('images/icon_my_true.png'),),
                label: "我的"
            ),
          ],
          currentIndex: _currentIndex,
          onTap: (int index) async{
            // if(index==2){
            //   Navigator.of(context).push(
            //       MaterialPageRoute(builder: (context)=>LoginPage()));
            //   _currentIndex=a;
            //   return;
            // }else{
            //   a=index;
            SharedPreferences prefs = await SharedPreferences.getInstance();
            int a=prefs.getInt("iflogin")??0;
            if(index == 2){
              _currentIndex=1;
              if(a==0){
                Navigator.of(context).push(MaterialPageRoute(builder: (context)=>LoginPage()));

                setState(() {

                });

              }else{
                _currentIndex= index;
              }

            }else{
              _currentIndex= index;
            }
              setState(() {

              });
            // }

          },
          type: BottomNavigationBarType.fixed,
        )
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    list
     ..add(IndexPage())
      ..add(CalculatorPage())
      ..add(MorePage())
    ..add(MinePage())
    ;
  }
}